/**
 * Copyright (C) 2010 Alfredo Morresi
 * 
 * This file is part of RainbowLibs project.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package it.rainbowbreeze.libs.helper;

import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Context;
import android.net.ConnectivityManager;

/**
 * @author Alfredo "Rainbowbreeze" Morresi
 *
 */
public class RainbowGlobalHelper {
	//---------- Constructors

	//---------- Private fields

	//---------- Public properties

	//---------- Public methods
	
	/**
	 * Verify is the device has generic connection
	 * http://stackoverflow.com/questions/1560788/how-to-check-internet-access-on-android-inetaddress-never-timeouts
	 * 
	 * Need this AndroidManifest permissions
	 *  uses-permission android:name="android.permission.INTERNET"
	 *  uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"
	 *  
	 * @return true if connection is present or in case of problems in connection detection
	 */
	public static boolean isOnline(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (null != cm && null != cm.getActiveNetworkInfo()) {
			return cm.getActiveNetworkInfo().isConnectedOrConnecting();
		} else {
			//default in case of problems
			return true;
		}
	}
	
	/**
	 * Verify is the device has specific connection to an host
	 * http://stackoverflow.com/questions/1560788/how-to-check-internet-access-on-android-inetaddress-never-timeouts
	 * 
	 * @param context
	 * @param hostToCheck: dns name of the host to check
	 * 
	 * @return true if connection is present or in case of problems in connection detection
	 */
	public static boolean isHostAvailable(Context context, String hostToCheck) {
        String completeHostName = 
            hostToCheck.toLowerCase().startsWith("http")
            ? hostToCheck
            : "http://" + hostToCheck;

        try {
            URL url = new URL(completeHostName);

            HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
            //HTC G1 user agent
            urlc.setRequestProperty("User-Agent", "tico e teco");
            urlc.setRequestProperty("Connection", "close");
            urlc.setConnectTimeout(10000);
            urlc.setReadTimeout(10000);
            urlc.connect();
            return true;
        //MalformedURLException, IOException and other possible exception
        } catch (Exception e) {
        }
        
        return false;
	}

	//---------- Private methods

}
